package com.ruoyi.system.service.impl;


import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.SysCustomer;
import com.ruoyi.system.domain.vo.CustomerVo;
import com.ruoyi.system.domain.vo.OptionVo;
import com.ruoyi.system.mapper.SysCustomerMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysCustomerService;
import com.yuweix.assist4j.core.DateUtil;
import com.yuweix.assist4j.dao.mybatis.where.Criteria;
import com.yuweix.assist4j.dao.mybatis.where.Operator;
import com.yuweix.assist4j.sequence.base.Sequence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 岗位信息 服务层处理
 * 
 * @author ruoyi
 */
@Service
public class SysCustomerServiceImpl implements ISysCustomerService {
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private SysCustomerMapper customerMapper;
    @Resource
    private Sequence seqCustomer;


    @Override
    public int queryCustomerCount(Long id, String customerNo, String enterpriseName, Long salesUserId
            , String keywords, Integer statusCode) {
        return customerMapper.queryCustomerCount(id, customerNo, enterpriseName, salesUserId
                , keywords, statusCode);
    }

    @Override
    public List<CustomerVo> queryCustomerList(Long id, String customerNo, String enterpriseName, Long salesUserId
            , String keywords, Integer statusCode, int pageNo, int pageSize) {
        List<SysCustomer> list = customerMapper.queryCustomerList(id, customerNo, enterpriseName, salesUserId
                , keywords, statusCode, pageNo, pageSize);
        return list == null || list.size() <= 0
                ? new ArrayList<>()
                : list.stream().map(this::toCustomerVo).collect(Collectors.toList());
    }
    private CustomerVo toCustomerVo(SysCustomer c) {
        if (c == null) {
            return null;
        }
        Long salesUserId = c.getSalesUserId();
        SysUser sysUser = salesUserId == null ? null : userMapper.selectUserById(salesUserId);

        CustomerVo vo = new CustomerVo();
        vo.setId(c.getId());
        vo.setCustomerNo(c.getCustomerNo());
        vo.setEnterpriseName(c.getEnterpriseName());
        vo.setContacts(c.getContacts());
        vo.setPhoneNo(c.getPhoneNo());
        vo.setTenantId(c.getTenantId());
        vo.setStatusCode(c.getStatusCode() == null ? null : String.valueOf(c.getStatusCode()));
        vo.setTypeCode(c.getTypeCode() == null ? null : String.valueOf(c.getTypeCode()));
        vo.setIndustryCode(c.getIndustryCode() == null ? null : String.valueOf(c.getIndustryCode()));
        vo.setRegionCode(c.getRegionCode() == null ? null : String.valueOf(c.getRegionCode()));
        vo.setFax(c.getFax());
        vo.setEmail(c.getEmail());
        vo.setZipCode(c.getZipCode());
        vo.setWebsite(c.getWebsite());
        vo.setAddr(c.getAddr());
        vo.setFpTitle(c.getFpTitle());
        vo.setFpBankName(c.getFpBankName());
        vo.setFpBankAccNo(c.getFpBankAccNo());
        vo.setFpPhoneNo(c.getFpPhoneNo());
        vo.setFpTaxNo(c.getFpTaxNo());
        vo.setFpAddr(c.getFpAddr());
        vo.setSalesUserId(salesUserId);
        vo.setSalesUserName(sysUser == null ? null : sysUser.getUserName());
        vo.setSalesNickName(sysUser == null ? null : sysUser.getNickName());
        vo.setSalesUserPhone(sysUser == null ? null : sysUser.getPhonenumber());
        vo.setSalesUserEmail(sysUser == null ? null : sysUser.getEmail());
        vo.setRemark(c.getRemark());
        vo.setCreator(c.getCreator());
        vo.setCreateTime(c.getCreateTime() == null ? "" : DateUtil.formatDate(c.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
        vo.setModifier(c.getModifier());
        vo.setModifyTime(c.getModifyTime() == null ? "" : DateUtil.formatDate(c.getModifyTime(), "yyyy-MM-dd HH:mm:ss"));
        return vo;
    }

    @Override
    public List<OptionVo> getCustomerOptionList(String keywords, int pageNo, int pageSize) {
        List<SysCustomer> list = customerMapper.queryCustomerList(null, null, null, null
                , keywords, null, pageNo, pageSize);
        return list == null || list.size() <= 0
                ? new ArrayList<>()
                : list.stream().map(c -> {
                    String customerNo = c.getCustomerNo();
                    String enterpriseName = c.getEnterpriseName();
                    return OptionVo.builder().value(customerNo).label("[" + customerNo + "]" + enterpriseName).build();
                }).collect(Collectors.toList());
    }

    @Override
    public CustomerVo findCustomerById(long id) {
        SysCustomer c = customerMapper.selectOneById(id, SysCustomer.class);
        return toCustomerVo(c);
    }

    @Override
    public CustomerVo findCustomerByNo(String customerNo) {
        List<SysCustomer> list = customerMapper.findList(Criteria.create("customer_no", Operator.eq, customerNo)
                , null, SysCustomer.class);
        return list == null || list.size() <= 0 ? null : toCustomerVo(list.get(0));
    }

    @Transactional
    @Override
    public long createCustomer(String customerNo, String enterpriseName, String contacts, String phoneNo
            , String tenantId, Integer statusCode, Integer typeCode, Integer industryCode, Integer regionCode
            , String fax, String email, String zipCode, String website, String addr
            , String fpTitle, String fpBankName, String fpBankAccNo, String fpPhoneNo, String fpTaxNo
            , String fpAddr, Long salesUserId, String remark, String accountNo) {
        List<SysCustomer> clist = customerMapper.findList(Criteria.create("customer_no", Operator.eq, customerNo)
                , null, SysCustomer.class);
        if (clist != null && clist.size() > 0) {
            throw new RuntimeException("数据[customerNo=" + customerNo + "]已存在");
        }

        SysCustomer c = new SysCustomer();
        c.setId(seqCustomer.nextValue());
        c.setCustomerNo(customerNo);
        c.setEnterpriseName(enterpriseName);
        c.setContacts(contacts);
        c.setPhoneNo(phoneNo);
        c.setTenantId(tenantId);
        c.setStatusCode(statusCode);
        c.setTypeCode(typeCode);
        c.setIndustryCode(industryCode);
        c.setRegionCode(regionCode);
        c.setFax(fax);
        c.setEmail(email);
        c.setZipCode(zipCode);
        c.setWebsite(website);
        c.setAddr(addr);
        c.setFpTitle(fpTitle);
        c.setFpBankName(fpBankName);
        c.setFpBankAccNo(fpBankAccNo);
        c.setFpPhoneNo(fpPhoneNo);
        c.setFpTaxNo(fpTaxNo);
        c.setFpAddr(fpAddr);
        c.setSalesUserId(salesUserId);
        c.setRemark(remark);
        c.setCreator(accountNo);
        c.setCreateTime(new Date());
        customerMapper.insertSelective(c);
        return c.getId();
    }

    @Transactional
    @Override
    public void updateCustomer(long id, String customerNo, String enterpriseName, String contacts, String phoneNo
            , String tenantId, Integer statusCode, Integer typeCode, Integer industryCode, Integer regionCode
            , String fax, String email, String zipCode, String website, String addr
            , String fpTitle, String fpBankName, String fpBankAccNo, String fpPhoneNo, String fpTaxNo
            , String fpAddr, Long salesUserId, String remark, String accountNo) {
        SysCustomer c = customerMapper.selectOneById(id, SysCustomer.class);
        if (c == null) {
            throw new RuntimeException("数据不存在[id=" + id + "]");
        }

        List<SysCustomer> clist = customerMapper.findList(Criteria.create("customer_no", Operator.eq, customerNo)
                , null, SysCustomer.class);
        if (clist != null && clist.size() > 0 && clist.get(0).getId() != id) {
            throw new RuntimeException("数据[customerNo=" + customerNo + "]已存在");
        }

        c.setCustomerNo(customerNo);
        c.setEnterpriseName(enterpriseName);
        c.setContacts(contacts);
        c.setPhoneNo(phoneNo);
        c.setTenantId(tenantId);
        c.setStatusCode(statusCode);
        c.setTypeCode(typeCode);
        c.setIndustryCode(industryCode);
        c.setRegionCode(regionCode);
        c.setFax(fax);
        c.setEmail(email);
        c.setZipCode(zipCode);
        c.setWebsite(website);
        c.setAddr(addr);
        c.setFpTitle(fpTitle);
        c.setFpBankName(fpBankName);
        c.setFpBankAccNo(fpBankAccNo);
        c.setFpPhoneNo(fpPhoneNo);
        c.setFpTaxNo(fpTaxNo);
        c.setFpAddr(fpAddr);
        c.setSalesUserId(salesUserId);
        c.setRemark(remark);
        c.setModifyTime(new Date());
        c.setModifier(accountNo);
        int i = customerMapper.updateByPrimaryKey(c);
        if (i < 0) {
            throw new RuntimeException("修改失败!");
        }
    }

    @Transactional
    @Override
    public void deleteCustomer(long id) {
        customerMapper.deleteByKey(id, SysCustomer.class);
    }
}
